<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ taglib uri="/WEB-INF/tlds/att.tld" prefix="att"%>
<html>

<head>
<meta name="decorator" content="moduleEdit" />
<script type="text/javascript" src="${staticPath}/js/decimalinput.js"></script>
<link rel="stylesheet" href="${staticPath}/lib/ztree/3.5/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script src="${staticPath}/js/jquery-dialog.js" type="text/javascript"></script>
<script src="${staticPath}/lib/ztree/3.5/js/jquery.ztree.all-3.5.js"type="text/javascript"></script>
<link href="${staticPath}/lib/chosen/1.1.0/chosen.min.css" rel="stylesheet" type="text/css">
<script src="${staticPath}/lib/chosen/1.1.0/chosen.jquery.js" type="text/javascript"></script>
<script src="${staticPath}/js/ossp-jquery-chosen.js" type="text/javascript"></script>
<style type="text/css">

</style>
<script type="text/javascript">
var max_tr_contact ='${fn:length(standard.listAssitems)}';


$(document).ready(function() {
	
	var opt = {
			service : "selectOneRegionService",
			idField : "areaId",
			nameField : "area",
			title : "地区选择对话框"
		};
	$("#areaDialog").Dialog(opt);
	
	
	var cmcate = {
			service : "selectCMCateDataService&cateType=01,02&cateflag=1",
			idField : "inviteCateClass",
			nameField : "cmcate",
			title : "招商分类(中类)选择对话框"
		};
	$("#cmcateDialog").Dialog(cmcate);
	

	var standtype=$('input[name="standardType"]:checked').val()
	
	if(standtype==1){
		$('#brandX').attr("disabled",false); 
		$("#brandLevel").attr("disabled",true); 
		$("#cmcateDialog").attr("disabled",'disabled'); 
		
	}else{
		$("#brandX").attr("disabled",true); 
		$("#brandLevel").attr("disabled");
		$("#cmcateDialog").removeAttr("disabled"); 
	}
	$("input[name^='standardType']").change(function() { 
		var v =$('input[name="standardType"]:checked').val()
		if(v==1){
			$('#brandX').attr("disabled",false); 
			$("#brandLevel").attr("disabled",true);
			$("#cmcateDialog").attr("disabled",'disabled'); 
		}else{
			$("#brandX").attr("disabled",true); 
			$("#brandLevel").attr("disabled",false);
			$("#cmcateDialog").removeAttr("disabled"); 
		}
	
	}); 
	
	
	$("#area").blur(
			function() {
						var areaId = $("#areaId").val();
						$.get(Com_Parameter.ContextPath + '/biz/contract/commonContract/' + areaId
								+ '/getStoreByAreaId',
								function(data) {
									var jsonObj = eval('(' + data + ')');
									$("#storeId").children("option").each(function() {
										$(this).remove();
									});
									$("#storeId").append(
											"<option value=''>---请选择---</option>");
									for (var i = 0; i < jsonObj.length; i++) {
										var shop = jsonObj[i];
										$("#storeId").append(
												"<option value='" + shop.code + "'>"
														+ shop.name + "</option>");
									}
								});
			});
	
	$("#storeId").change(function() {
				var storeId = $("#storeId").val();
				var store;
				$(this).children("option").each(function() {
					if ($(this).val() == storeId) {
						store = $(this).text();
					}
				});
				$("#store").val(store);
	});
	
	
	var area = $("#area").val();
	if (area != "" && area != null) {
		var areaId = $("#areaId").val();
		$.get(Com_Parameter.ContextPath + '/biz/contract/commonContract/' + areaId
				+ '/getStoreByAreaId', function(data) {
			var jsonObj = eval('(' + data + ')');
			for (var i = 0; i < jsonObj.length; i++) {
				var shop = jsonObj[i];
				$("#storeId").append("<option value='" + shop.code + "'>" + shop.name+ "</option>");
				$("#storeId").children("option").each(function() {
					if ($(this).val() == '${standard.storeId}') {
						$(this).attr("selected", "selected");
					}
				});
			}
		});
	}
	$("#basicRateMin").decimalinput();
	$("#basicRateMax").decimalinput();
	$("#overRateMin").decimalinput();
	$("#overRateMax").decimalinput();
	$("#contractEffect").decimalinput();
	
	
	$("#standardForm").validate({
		submitHandler: function(form){
			var basicRateMin =$("#basicRateMin").val();
			var basicRateMax=$("#basicRateMax").val();
			var standardType=$('input[name="standardType"]:checked').val();
			if($("#standardName").val() ==''||$("#standardName").val()==null){
				top.$.jBox.tip('标准名称 不能为空', 'warning');
				return;
			}
			
			if($("#areaId").val() ==''||$("#areaId").val()==null){
				top.$.jBox.tip('区域 不能为空', 'warning');
				return;
			}
			
			if($("#storeId").val() ==''||$("#storeId").val()==null){
				top.$.jBox.tip('门店 不能为空', 'warning');
				return;
			}
			
			if($("#corpType").val() ==''||$("#corpType").val()==null){
				top.$.jBox.tip('合作类型 不能为空', 'warning');
				return;
			}
			
			if(basicRateMin ==''||basicRateMin==null){
				top.$.jBox.tip('基扣 下限不能为空', 'warning');
				return;
			}
			if(basicRateMax==''||basicRateMax==null){
				top.$.jBox.tip('基扣 上限不能为空', 'warning');
				return;
			}
			if(basicRateMin>basicRateMax){
				top.$.jBox.tip('基扣 上限要大于下线', 'warning');
				return;
			}
			
			if($("#overRateMax").val() ==''||$("#overRateMax").val()==null){
				top.$.jBox.tip('超扣标准上线不能为空', 'warning');
				return;
			}
			
			if($("#overRateMin").val() ==''||$("#overRateMin").val()==null){
				top.$.jBox.tip('超扣标准下线不能为空', 'warning');
				return;
			}
			
			if($("#overRateMax").val()<$("#overRateMin").val()){
				top.$.jBox.tip('超扣标准上线限要大于下线', 'warning');
				return;
			}
			if(standardType==null||standardType==''){
				top.$.jBox.tip('请选择标准类型', 'warning');
				return;
			}
	        if(standardType==1){
	        	
	        	if($("#brandId").val() ==''||$("#brandId").val()==null){
					top.$.jBox.tip('品牌不能为空', 'warning');
					return;
				}
				
	        }else{
	        	if($("#inviteCateClass").val() ==''||$("#inviteCateClass").val()==null){
					top.$.jBox.tip('招商分类不能为空', 'warning');
					return;
				}
	        	if($("#brandLevel").val() ==''||$("#brandLevel").val()==null){
					top.$.jBox.tip('品牌等级不能为空', 'warning');
					return;
				}
	        }
	        if($("#startDate").val() ==''||$("#startDate").val()==null){
				top.$.jBox.tip('开始日期不能为空', 'warning');
				return;
			}
			
			if($("#endDate").val() ==''||$("#endDate").val()==null){
				top.$.jBox.tip('结束日期不能为空', 'warning');
				return;
			}
			
			if($("#startDate").val()>$("#endDate").val()){
				top.$.jBox.tip('开始日期小于结束日期', 'warning');
				return;
			}
	
			var flag=true
			$("#codeTable tbody tr").each(function() {
				var inputs = $(this).find("input");
				var selects = $(this).find("select");
				try {
					
					var amountMaxValue='';
					var amountMinValue='';
					var _index=''
					selects.each(function() {
						var index = $(this).attr("data_index");
						var deductCodeId = 'listAssitems'+ index+ '.deductCode';
						if(index!=''&&index!=null){
							var ui = $(this);
							if(ui.attr("value") ==''||ui.attr("value")==null){
								throw "第"+_index+"扣款项目不能为空";
							}
						}						
					});
					inputs.each(function() {
						var index = $(this).attr("data_index");
						if(index!=''&&index!=null){
							var amountMaxId = 'listAssitems'+ index+ '.amountMax';
							var amountMinId = 'listAssitems'+ index+ '.amountMin';
							var ui = $(this);
							_index=parseInt(index)+1;
							if(this.id==amountMaxId){
								if(ui.attr("value") ==''||ui.attr("value")==null){
									throw "第"+_index+"行上线不能为空";
								}else{
									amountMaxValue=ui.attr("value")
								}
							}
							if(this.id==amountMinId){
								if(ui.attr("value") ==''||ui.attr("value")==null){
									throw "第"+_index+"行下线不能为空";
								}else{
									amountMinValue=ui.attr("value")
								}
							}
						}
					});
					if(amountMaxValue<amountMinValue){
						throw "第"+_index+"上线小于下线";
					}
				}catch (e) {
		            top.$.jBox.tip(e, 'warning');
		            flag=false;
		            return  false;
		        }
	
			})
			if(!flag){
				return;
			}
			Oosp_Loading('正在提交，请稍等...');
			form.submit();
		},
		errorContainer: "#messageBox",
		errorPlacement: function(error, element) {
			$("#messageBox").text("输入有误，请先更正。");
			if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
				error.appendTo(element.parent().parent());
			} else {
				error.insertAfter(element);
			}
		}
	});


});
/*******************************
 * 临时条款添加
 *******************************/
function add_hidden(tId, tList) {
	var tr = $(tId).clone();
	var table = $(tId).closest("table");
	// 替换所有的hidden，方便spring的form绑定
	var inputs = tr.find("input");
	var selects = tr.find("select");
	if (tId == "#hidden_contact") {
		max_tr_contact++;
	}
	inputs.each(function() {
		if (tId == "#hidden_contact") {
			this.id = tList + max_tr_contact + this.id;
			this.name = tList + "[" + max_tr_contact + "]" + this.name;
			$(this).attr("data_index", "" + max_tr_contact + "");
		}

	});
	selects.each(function() {
		if (tId == "#hidden_contact") {
			var select = this;
			var ui = $(this);
			this.id = tList + max_tr_contact + this.id;
			this.name = tList + "[" + max_tr_contact + "]" + this.name;
			$(this).attr("data_index", "" + max_tr_contact + "");
		}

	});
	tr.show();
	table.append(tr);
}

/*******************************
 * 临时条款删除
 *******************************/
function delete_tr(t, dList) {
	var tr = $(t).closest("tr");
	var intputss = tr.find("input :first")
	var id = tr.find("input :first").val();
	if (id != 0) {
		var val = $(dList).val();
		if (val == "") {
			$(dList).val(id);
		} else {
			$(dList).val(val + "," + id);
		}
	}
	tr.remove();

}

function openTempleteSelector(url,iWidth,iHeight){
	var iTop = (window.screen.availHeight - iHeight) / 2;  
    var iLeft = (window.screen.availWidth - iWidth) / 2;  
	var returnValue = window.showModalDialog(url,'',"dialogTop:"+iTop+"px;dialogLeft:"+iLeft+"px;location=no;center:Yes;dialogHeight:"+iHeight+"px;dialogWidth:"+iWidth+"px;status:No;help:No;");
	return eval('(' + returnValue + ')');
}

function openWin(url){
	var returnValue = openTempleteSelector(url,600,550);
	$("#brandName").val(returnValue.brandInfo);
	$("#brandId").val(returnValue.brandCateId);
	
	
}
</script>
</head>
<body>

	<div class="Report_f">
		<div class="Report_f_1">
			<form:form id="standardForm" modelAttribute="standard"  action="${contextPath}/biz/satndardAssort//save?navId=${navId}" method="post" class="form-horizontal" role="form">
				<div class="Report_f_title_1">配套招商标准-新增</div>
					<div class="tab-content Report_f_title_3 ">
						<div class="Report_f_title_4">
							<form:hidden path="createBy" />
							<form:hidden path="id" />
							<form:hidden path="opeateStatus"/>
							<input type="hidden" name="delContactId" id="delContactId">
						<div class="f_2">
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">标准名称:</label>
										<div class="col-sm-6 f_6">
											<form:input path="standardName" id="standardName"
												maxlength="200" class="f_5 " />
										</div>
								</div>
								<div class="form-group col-sm-6">
									
								</div>
						</div>
						
					
							<div class="f_2">
									<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">管理区域:</label>
										<div class="col-sm-6 f_6">
											<form:hidden id="areaId" path="areaId" />
											<div class="input-group">
												<input type="text" class="f_5" value="${standard.area}" id="area" name="area" readonly="readonly"> 
												<span class="input-group-btn">
													<button class="btn btn-default btn_3" id="areaDialog" type="button">选择</button>
												</span>
											</div>
										</div>
									</div>
									<div class="form-group col-sm-6">
										<div>
											<label for="inputtext3" class="col-sm-3 control-label f_4">门店:</label>
											<div class="col-sm-6 f_6">

												<select class="f_5" id="storeId" name="storeId">
													<option value="">---请选择---</option>
												</select>
												<form:hidden path="store" id="store" name="store" />

											</div>
										</div>
									</div>
								</div>
					
						<div class="f_2">
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">合作类型:</label>
										<div class="col-sm-6 f_6">
											<ossp:select dictType="SUPPIER_OPERATION"  id="corpType"  value="${standard.corpType}"  name="corpType">
											
											</ossp:select>
											
										</div>
								</div>
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">租金平效:</label>
										<div class="col-sm-6 f_6">
											<input type="text" name="contractEffect" value="<fmt:formatNumber value="${standard.contractEffect}" pattern="0.00#"/>"
											 id="contractEffect" maxlength="200" class="f_5 " />
										</div>
								</div>
						</div>
						<div class="f_2">
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">基扣 上限:</label>
										<div class="col-sm-6 f_6">
											<input type="text" name="basicRateMax" value="<fmt:formatNumber value="${standard.basicRateMax}" pattern="0.00#"/>"
											 id="basicRateMax" maxlength="200" class="f_5 " />
								
										</div>
								</div>
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">基扣 下限:</label>
										<div class="col-sm-6 f_6">
										<input type="text" name="basicRateMin" value="<fmt:formatNumber value="${standard.basicRateMin}" pattern="0.00#"/>"
											 id="basicRateMin" maxlength="200" class="f_5 " />
									
										</div>
								</div>
						</div>
						
						<div class="f_2">
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">超扣标准上线:</label>
										<div class="col-sm-6 f_6">
									
												<input type="text" name="overRateMax" value="<fmt:formatNumber value="${standard.overRateMax}" pattern="0.00#"/>"
											 id="overRateMax" maxlength="200" class="f_5 " />
										</div>
								</div>
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">超扣标准下线:</label>
										<div class="col-sm-6 f_6">
											<input type="text" name="overRateMin" value="<fmt:formatNumber value="${standard.overRateMin}" pattern="0.00#"/>"
											 id="overRateMin" maxlength="200" class="f_5 " />
										
										</div>
								</div>
						</div>
						<div class="f_2">
						
								<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">标准类型:</label>
										<div class="col-sm-6 f_6">
											
											<ossp:radio dictType="standardType" defaultChecked="${standard.standardType}" id="standardType" name="standardType"  ></ossp:radio>
											
										</div>
								</div>
						
							<div class="form-group col-sm-6">
								<label for="brand.brandEname" class="col-sm-3 control-label f_4">品牌:</label>
								<div class="col-sm-6 f_6">
									<div class="input-group">
									<input type="hidden" id="brandId"  name="brandId" value="${standard.brandId}" readonly="readonly" class="f_5 "/>
									<input  id="brandName"  name="brandName" value="${standard.brandName}" readonly="readonly" class="f_5 "/>
									<span class="input-group-btn">
									<a class="btn btn-default btn_3" disabled="disabled" name="brandX" id="brandX" href="#" onclick="openWin('${contextPath}/biz/brand/brandcate/dialoglist')">选择</a>
									</span>
							    </div>
								</div>
							</div>
								
							
						</div>
					<div class="f_2">
							<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">招商分类:</label>
										<div class="col-sm-6 f_6">
											<form:hidden id="inviteCateClass" path="inviteCateClass" />
											<div class="input-group">
												<input type="text" class="f_5" value="${standard.cmcate.cateName}" id="cmcate"  readonly="readonly"> 
												<span class="input-group-btn">
													<button class="btn btn-default btn_3" disabled="true" id="cmcateDialog" type="button">选择</button>
												</span>
											</div>
										</div>
									</div>
							<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">品牌等级:</label>
										
										<div  class="col-sm-6 f_6" >
										
											<ossp:select disabled="disabled" service="selectBrandLevelDataService" id="brandLevel" name="brandLevel" defaultSelected="${standard.brandLevel}"></ossp:select>
										</div>
										
							 </div>
						</div>
						<!-- *************************************************** -->	
						<div class="f_2">

									<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">起始日期:</label>

										<div class="col-sm-7">
											<div class="input-group date startDate "  data-date-format="yyyy-mm-dd">
												<input class="f_5" readonly="readonly" id="startDate" type="text" name="startDate" value="<fmt:formatDate value="${standard.startDate}"  pattern="yyyy-MM-dd"/>" size="10"  /> 
												<span class="input-group-addon">
												<span
													class="glyphicon glyphicon-remove"></span></span> <span
													class="input-group-addon"><span
													class="glyphicon glyphicon-calendar"></span></span>

											</div>
										</div>
									</div>

									<div class="form-group col-sm-6">
										<label for="inputtext3" class="col-sm-3 control-label f_4">终止日期:</label>
										<div class="col-sm-7">
											<div class="input-group date endDate "  data-date-format="yyyy-mm-dd">
												<input class="f_5" id="endDate"  name="endDate" size="10"  readonly="readonly" type="text"  value="<fmt:formatDate value="${standard.endDate}"   pattern="yyyy-MM-dd"/>" />
												<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span> 
												<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
											</div>
											<span id="endDateMessage" class=" help-inline" style="color: red;"></span>
										</div>
									</div>

								</div>
						
						<div class="f_2">
									<h4>
										扣款项 <input
											onclick="add_hidden('#hidden_contact','listAssitems')"
											type="button" class="btn btn-success btn-sm pull-right"
											value="添加"></input>
									</h4>

									<table class="table table-striped table-hover o-l-table" id="codeTable">
									<thead>
											<tr>
												<th>扣款项<span style="color: red;" style="width:150px">*</span></th>
												<th style="width: 150px">上限</th>
												<th style="width: 150px">下限</th>
												<th style="width: 150px">面积上限</th>
												<th style="width: 150px">面积下限</th>
												<th>操作</th>
											</tr>
										</thead>
									<tbody>
													
									<c:forEach items="${assItemsList}" var="items" varStatus="i">
										<tr>
											 <td class="o-l-tab-ckb">
											 <input type="hidden" value="${items.id}" id="listAssitems${i.index}.id" name="listAssitems[${i.index}].id">
											 	<ossp:select  htmlEleProperties="data_index=${i.index}" value="${items.deductCode}" service="selectArtictService?contractType=all" id="listAssitems${i.index}.deductCode" name="listAssitems[${i.index}].deductCode" >
												</ossp:select>
											</td>
											<td class="o-l-tab-ckb">
											
											<input class="f_5" data_index="${i.index}" type="text"id="listAssitems${i.index}.amountMax" 
											value="<fmt:formatNumber value="${items.amountMax}" pattern="0.00#"/>" name="listAssitems[${i.index}].amountMax">
											</td>
											<td class="o-l-tab-ckb">
												<input class="f_5" data_index="${i.index}"  type="text"id="listAssitems${i.index}.amountMin"
												 name="listAssitems[${i.index}].amountMin"  value="<fmt:formatNumber value="${items.amountMin}" pattern="0.00#"/>">
											</td>
											<td>
											<input class="f_5" data_index="${i.index}" type="text"id="listAssitems${i.index}.areaMax" 
											value="<fmt:formatNumber value="${items.areaMax}" pattern="0.00#"/>" name="listAssitems[${i.index}].areaMax">
											</td>
											<td class="o-l-tab-ckb">
												<input class="f_5" data_index="${i.index}"  type="text"id="listAssitems${i.index}.areaMin"
												 name="listAssitems[${i.index}].areaMin"  value="<fmt:formatNumber value="${items.areaMin}" pattern="0.00#"/>">
											</td>
											<td><input type="button" onclick="delete_tr(this,'#delContactId')" value="删除" class="btn btn-default btn-sm"></td>
										</tr>
									</c:forEach>
										
										<tr style="display: none" id="hidden_contact">
											 <td class="o-l-tab-ckb">
												<ossp:select service="selectArtictService?contractType=all" id=".deductCode" name=".deductCode" >
												</ossp:select>
											</td>
											<td class="o-l-tab-ckb"><input class="f_5" type="text"id=".amountMax" name=".amountMax"></td>
											<td class="o-l-tab-ckb"><input class="f_5" type="text"id=".amountMin" name=".amountMin"></td>
											<td class="o-l-tab-ckb"><input class="f_5" type="text"id=".areaMax" name=".amountMax"></td>
											<td class="o-l-tab-ckb"><input class="f_5" type="text"id=".areaMin" name=".amountMin"></td>
											<td><input type="button"
													onclick="delete_tr(this,'#delContactId')" value="删除"
													class="btn btn-default btn-sm"></td>
											</tr>
									</tbody>
									
								</table>
						
					</div>
				</div>	
				<div class="f_12">
					<a class="btn btn-danger btn_5 btn_right"  href="${contextPath}/biz/satndardAssort/list?navId=${navId}">返回</a>
					<button id="save" class="btn btn-success btn_5 btn_right" type="submit">保 存</button>
				</div>
					</div>
				</form:form>
				
	</div>
		</div>

<script type="text/javascript">
	$('.startDate').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		format: 'yyyy-mm-dd'
    })

	$('.endDate').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		format: 'yyyy-mm-dd'
    })

</script>
</body>
</html>